//
//  XLChatInputPanelUser.m
//  XiangLove
//
//  Created by 李传坤 on 2018/4/18.
//  Copyright © 2018年 BAOMIHUA.COM. All rights reserved.
//

#import "XLChatInputPanelUser.h"
#import "UIView+Layout.h"
#import "NSObject+FBKVOController.h"


@implementation XLChatInputPanelUser

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [self.KVOController unobserveAll];
}

- (instancetype)initWithCahtType:(BOOL)type andUser:(IMAUser *)user
{
    if (self = [super init])
    {
        
        [self initSourceAndTools:type user:user];
    }
    return self;
}

- (instancetype)initWithchatType:(BOOL)type;
{
    if (self = [super init])
    {
        
        [self initSourceAndTools:type user:nil];
    }
    return self;
}

//初始化一些数据
- (void)initSourceAndTools:(BOOL)type user:(IMAUser *)user
{
    _type = type;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onKeyboardDidShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onKeyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onKeyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onKeyboardDidShow:) name:UIKeyboardDidChangeFrameNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(chatDisappear) name:@"chatDisappear" object:nil];
    
    _toolBar = [[XLChatInputToolBarUser alloc] initWithChatType:_type user:user];
//    _toolBar = [[XLChatInputToolBarUser alloc] initWithchatType:_type];
    _toolBar.backgroundColor = kWhiteColor;
    //        _toolBar.backgroundColor = [UIColor blueColor];
    _toolBar.toolBarDelegate = self;
    [self addSubview:_toolBar];
    
    self.KVOController = [FBKVOController controllerWithObserver:self];
    __weak XLChatInputPanelUser *ws = self;
    [self.KVOController observe:_toolBar keyPath:@"contentHeight" options:NSKeyValueObservingOptionNew |NSKeyValueObservingOptionOld block:^(id observer, id object, NSDictionary *change) {
        [ws onToolBarContentHeightChanged:change];
    }];
}

- (void)chatDisappear {
    [self endEditing:YES];
    [self onHideAddtionalPanel:_panel completion:^{
        [self onSwitchPanel];
    }];
    
}
- (instancetype)initRichChatInputPanel
{
    if (self = [self init])
    {
        
        [self.KVOController unobserve:_toolBar keyPath:@"contentHeight"];
        [_toolBar removeFromSuperview];
        
        _toolBar = [[RichChatInputToolBar alloc] init];
        _toolBar.toolBarDelegate = self;
        [self addSubview:_toolBar];
        
        __weak XLChatInputPanelUser *ws = self;
        [self.KVOController observe:_toolBar keyPath:@"contentHeight" options:NSKeyValueObservingOptionNew |NSKeyValueObservingOptionOld block:^(id observer, id object, NSDictionary *change) {
            [ws onToolBarContentHeightChanged:change];
        }];
    }
    return self;
}

- (void)setInputText:(NSString *)text
{
    [_toolBar setInputText:text];
}

- (void)setChatDelegate:(id<ChatInputAbleViewDelegate>)delegate
{
    _chatDelegate = delegate;
    _toolBar.chatDelegate = delegate;
}
- (void)sendAudioRecord:(ChatAudioRecords *)audio {
    IMAMsg *msg = [IMAMsg msgWithSoundPath:audio.filePath duration:audio.duration];
//    IMAMsg *msg = [IMAMsg msgWithSound:audio.audioData duration:audio.duration];
    if ([self.chatDelegate respondsToSelector:@selector(onChatInput:sendMsg:)])
    {
        [self.chatDelegate onChatInput:nil sendMsg:msg];
    }
    
}
- (void)onKeyboardWillHide:(NSNotification *)notification
{
    NSDictionary* userInfo = [notification userInfo];
    CGFloat duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    
    NSInteger contentHeight = [_toolBar contentHeight] + [_panel contentHeight];
    
    if (_contentHeight != contentHeight)
    {
        CGRect rect = self.frame;
        if (IS_IPHONE_X) {
            rect.origin.y = kMainScreenHeight - 88 - contentHeight - 34;
        } else {
            rect.origin.y = kMainScreenHeight - 64 - contentHeight;
        }
        rect.size.height = contentHeight;
        
        [UIView animateWithDuration:duration animations:^{
            self.frame = rect;
            self.contentHeight = contentHeight;
        }];
        
    }
    _panel.hidden = NO;
}


- (void)onKeyboardDidShow:(NSNotification *)notification
{
    if ([_toolBar isEditing])
    {
        NSDictionary *userInfo = notification.userInfo;
        CGRect endFrame = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
        CGFloat duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
        
        NSInteger contentHeight = endFrame.size.height + [_toolBar contentHeight];
        
        if (contentHeight != _contentHeight)
        {
            CGRect rect = self.frame;
            if (IS_IPHONE_X) {
                rect.origin.y = endFrame.origin.y - [_toolBar contentHeight] - 88;
            } else {
                rect.origin.y = endFrame.origin.y - [_toolBar contentHeight] - 64;
            }
            rect.size.height = contentHeight;
            
            [UIView animateWithDuration:duration animations:^{
                self.frame = rect;
                self.contentHeight = contentHeight;
            }];
        }
        _panel.hidden = YES;
    }
}


- (void)addOwnViews
{
}

- (void)onToolBarContentHeightChanged:(NSDictionary *)change
{
    NSInteger nv = [change[NSKeyValueChangeNewKey] integerValue];
    NSInteger ov = [change[NSKeyValueChangeOldKey] integerValue];
    if (nv != ov)
    {
        NSInteger off = nv - ov;
        CGRect rect = self.frame;
        rect.origin.y -= off;
        rect.size.height += off;
        
        self.frame = rect;
        [UIView animateWithDuration:0.25 animations:^{
            self.contentHeight = _contentHeight + off;
        }];
    }
    
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    [self relayoutFrameOfSubViews];
}

- (void)relayoutFrameOfSubViews
{
    CGRect rect = self.bounds;
    [_toolBar sizeWith:CGSizeMake(rect.size.width, _toolBar.contentHeight)];
    [_toolBar relayoutFrameOfSubViews];
    
    [_panel setFrameAndLayout:CGRectMake(0, _toolBar.contentHeight, rect.size.width, rect.size.height - _toolBar.contentHeight)];
}

- (BOOL)resignFirstResponder
{
    [_toolBar resignFirstResponder];
    [self onHideAddtionalPanel:_panel completion:^{
        [self onSwitchPanel];
    }];
    return [super resignFirstResponder];
}

- (void)onSwitchPanel
{
    if (_panel)
    {
        [_panel removeFromSuperview];
    }
    //    _contentHeight = 100;
    
    _panel = nil;
}


#pragma mark -  toolBar 上button点击代理
#pragma mark - 点击录音  发送语音
- (void)onToolBarClickAudio:(XLChatInputToolBarUser *)bar show:(BOOL)isShow {
    if (isShow)
    {
        if (!_funcPanel)
        {
            chatAudioPanel  *audioPanel = [[chatAudioPanel alloc] init];
            audioPanel.chatDelegate = self.chatDelegate;
            audioPanel.delegate = self;
            _audioPanel = audioPanel;
            
        }
        
        [self onShowPanel:_audioPanel];
    }
    else
    {
        [self onHideAddtionalPanel:_panel completion:^{
            [self onSwitchPanel];
        }];
    }
    
}

#pragma mark - 点击表情  发送表情
- (void)onToolBarClickEmoj:(XLChatInputToolBarUser *)bar show:(BOOL)isShow
{
    if (isShow)
    {
        if (!_emojPanel)
        {
            ChatEmojView *emojPanel = [[ChatEmojView alloc] init];
            emojPanel.chatDelegate = self.chatDelegate;
            emojPanel.delegate = _toolBar;
            _emojPanel = emojPanel;
        }
        
        [self onShowPanel:_emojPanel];
    }
    else
    {
        [self onHideAddtionalPanel:_panel completion:^{
            [self onSwitchPanel];
        }];
    }
}

#pragma mark - 点击照片  发送照片
- (void)onToolBarClickPhoto:(XLChatInputToolBarUser *)bar show:(BOOL)isShow
{
    if (isShow)
    {//显示选择照片的界面
        if ([_chatDelegate respondsToSelector:@selector(onChatInputSendFreeImage:)])
        {
            [_chatDelegate onChatInputSendFreeImage:self];
        }
    }
    else
    {
        [self onHideAddtionalPanel:_panel completion:^{
            [self onSwitchPanel];
        }];
    }
}

#pragma mark - 点击视频  发送小视频
- (void)onToolBarClickVideo:(XLChatInputToolBarUser *)bar show:(BOOL)isShow
{
    if (isShow)
    {//显示拍摄小视频的界面
        if ([_chatDelegate respondsToSelector:@selector(onChatInputSendMoneyVideo:)])
        {
            [_chatDelegate onChatInputSendMoneyVideo:self];
        }
    }
    else
    {
        [self onHideAddtionalPanel:_panel completion:^{
            [self onSwitchPanel];
        }];
    }
}

#pragma mark - 点击电话  拨打电话
- (void)onToolBarClickPhoneChat:(XLChatInputToolBarUser *)bar show:(BOOL)isShow
{
    if (isShow)
    {//显示拨打界面
        if ([_chatDelegate respondsToSelector:@selector(onChatInputChatVoice:)])
        {
            [_chatDelegate onChatInputChatVoice:self];
        }
    }
    else
    {
        [self onHideAddtionalPanel:_panel completion:^{
            [self onSwitchPanel];
        }];
    }
}

- (void)onShowAudioPanel:(UIView<ChatInputAbleView> *)panel
{
    NSInteger oldPanelContentHeight = 0;
    [self onSwitchPanel];
    
    NSInteger contentHeight = [panel contentHeight];
    [panel setFrameAndLayout:CGRectMake(0, 0, self.bounds.size.width, contentHeight)];
    [self addSubview:panel];
    _panel = panel;
    [self onShowAddtionalPanel:panel withOff:contentHeight - oldPanelContentHeight];
}

- (void)onShowPanel:(UIView<ChatInputAbleView> *)panel
{
    NSInteger oldPanelContentHeight = [_panel contentHeight];
    [self onSwitchPanel];
    
    NSInteger contentHeight = [panel contentHeight];
    [panel setFrameAndLayout:CGRectMake(0, 0, self.bounds.size.width, contentHeight)];
    [self addSubview:panel];
    _panel = panel;
    
    [self onShowAddtionalPanel:panel withOff:contentHeight - oldPanelContentHeight];
}

#pragma mark - ToolBar 点击更多按钮
- (void)onToolBarClickMore:(XLChatInputToolBarUser *)bar show:(BOOL)isShow
{
    if (isShow)
    {
        if (!_funcPanel)
        {
            _funcPanel = [[ChatFunctionPanel alloc] init];
            _funcPanel.chatDelegate = self.chatDelegate;
        }
        
        [self onShowPanel:_funcPanel];
    }
    else
    {
        [self onHideAddtionalPanel:_panel completion:^{
            [self onSwitchPanel];
        }];
    }
    
}
#pragma mark - ToolBar 点击付费图片按钮
- (void)onToolBarClickPayimage:(XLChatInputToolBarUser *)bar show:(BOOL)isShow
{
    if (isShow)
    {
        //        if (!_funcPanel)
        //        {
        //            _funcPanel = [[ChatFunctionPanel alloc] init];
        //            _funcPanel.chatDelegate = self.chatDelegate;
        //        }
        //
        //        [self onShowPanel:_funcPanel];
    }
    else
    {
        //        [self onHideAddtionalPanel:_panel completion:^{
        //            [self onSwitchPanel];
        //        }];
    }
    if ([_chatDelegate respondsToSelector:@selector(onChatInputSendMoneyImage:)])
    {
        [_chatDelegate onChatInputSendMoneyImage:self];
    }
    
}
#pragma mark - ToolBar 点击免费图片按钮
- (void)onToolBarClickFreeimage:(XLChatInputToolBarUser *)bar show:(BOOL)isShow
{
    if ([_chatDelegate respondsToSelector:@selector(onChatInputSendFreeImage:)])
    {
        [_chatDelegate onChatInputSendFreeImage:self];
    }
    
}

#pragma mark - ToolBar 点击付费小视频按钮
- (void)onToolBarClickPayVideo:(XLChatInputToolBarUser *)bar show:(BOOL)isShow
{
    if ([_chatDelegate respondsToSelector:@selector(onChatInputSendMoneyVideo:)])
    {
        [_chatDelegate onChatInputSendMoneyVideo:self];
    }
    
}
- (void)onToolBarClickChatVideo:(XLChatInputToolBarUser *)bar show:(BOOL)isShow
{
    if ([_chatDelegate respondsToSelector:@selector(onChatInputChatVideo:)])
    {
        [_chatDelegate onChatInputChatVideo:self];
    }
    
}

#pragma mark - 拨打视频电话
- (void)onToolBarClickFlower:(XLChatInputToolBarUser *)bar show:(BOOL)isShow
{
    
    if (isShow)
    {//显示拨打界面
        if ([_chatDelegate respondsToSelector:@selector(onChatInputChatVideo:)])
        {
            [_chatDelegate onChatInputChatVideo:self];
        }
    }
    else
    {
        [self onHideAddtionalPanel:_panel completion:^{
            [self onSwitchPanel];
        }];
    }
    
}

- (void)onShowAddtionalPanel:(UIView<ChatInputAbleView> *)panel withOff:(NSInteger)offer
{
    if (offer == 0)
    {
        // 说明没有切换
        return;
    }
    
    CGRect rect = self.frame;
    rect.origin.y -= offer;
    rect.size.height += offer;
    
    [UIView animateWithDuration:0.25 animations:^{
        self.frame = rect;
        self.contentHeight += offer;
    }];
    
    
}

- (void)onHideAddtionalPanel:(UIView<ChatInputAbleView> *)panel completion:(NormalVoidBlock)block
{
    NSInteger contentHeight = [panel contentHeight];
    CGRect rect = self.frame;
    rect.origin.y += contentHeight;
    rect.size.height -= contentHeight;
    
    [UIView animateWithDuration:0.25 animations:^{
        self.frame = rect;
        self.contentHeight -= contentHeight;
    } completion:^(BOOL finished) {
        if (block)
        {
            block();
        }
    }];
}

- (IMAMsg *)getMsgDraft
{
    return [(RichChatInputToolBar *)_toolBar getMsgDraft];
}

- (void)setMsgDraft:(IMAMsg *)draft
{
    [(RichChatInputToolBar *)_toolBar setMsgDraft:draft];
}

@end



@implementation RichChatInputPanelUser

- (void)onToolBarClickEmoj:(XLChatInputToolBarUser *)bar show:(BOOL)isShow
{
    if (isShow)
    {
        if (!_emojPanel)
        {
            ChatSystemFaceView *emojPanel = [[ChatSystemFaceView alloc] init];
            emojPanel.chatDelegate = self.chatDelegate;
            emojPanel.inputDelegate = _toolBar;
            _emojPanel = emojPanel;
        }
        
        [self onShowPanel:_emojPanel];
    }
    else
    {
        [self onHideAddtionalPanel:_panel completion:^{
            [self onSwitchPanel];
        }];
    }
}

@end

